﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TestMySqlConnetPoll;
using View.Common;
using System.Threading;

namespace View.Journal
{
    /// <summary>
    /// 用户浏览记录控件
    /// </summary>
    public partial class ControlCountForm : DevExpress.XtraEditors.XtraForm
    {
        //用户浏览记录Business
        private UserScanTable uScanTable = new UserScanTable();

        public ControlCountForm()
        {
            InitializeComponent();
            SetTimeList();

            this.grid_main.DataSource = viewList;
        }

        private void SetTimeList()
        {
            this.comboBox_time.Items.Clear();
            DateTime dt = DateTime.Now;

            for (int i = 1; i <= 60; i++)
            {
                this.comboBox_time.Items.Add(UtilityFunc.DateTimeStringFormat(dt, TimeFormatType.DAY));
                dt = dt.AddDays(-1);
            }

            this.comboBox_time.SelectedIndex = 0;
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnReflash_Click(object sender, EventArgs e)
        {
            GetDataSource();
        }

        /// <summary>
        /// 查询对应数据
        /// </summary>
        private void GetDataSource()
        {
            while (!uScanTable.Ready_ValueList)
            {
                if (uScanTable.ErrorType != 0)
                    return;

                Thread.Sleep(20);
            }

            foreach (string strKey in uScanTable.ValueList.Keys)
            {
                ControlCount view = viewList.SingleOrDefault(v => v.name == strKey);
                int rowIndex = viewList.IndexOf(view);
                int index = 0;
                foreach (objTableScanAnnal tab in uScanTable.ValueList[strKey])
                {
                    DateTime dts = MySql.Time_tToDateTime(tab.s_time);
                    DateTime dte = MySql.Time_tToDateTime(tab.e_time);
                    if (index == 0)
                    {
                        view.openTime = UtilityFunc.DateTimeStringFormat(dts, TimeFormatType.DCGONGLV);
                        view.closeTime = UtilityFunc.DateTimeStringFormat(dte, TimeFormatType.DCGONGLV);
                        view.totalTime = dte.Subtract(dts).Hours + ":" + dte.Subtract(dts).Minutes + ":" + dte.Subtract(dts).Seconds;
                    }
                    else
                    {
                        ControlCount viewTemp = new ControlCount();
                        viewTemp.name = view.name;
                        viewTemp.openTime = UtilityFunc.DateTimeStringFormat(dts, TimeFormatType.DCGONGLV);
                        viewTemp.closeTime = UtilityFunc.DateTimeStringFormat(dte, TimeFormatType.DCGONGLV);
                        viewTemp.totalTime = dte.Subtract(dts).Hours + ":" + dte.Subtract(dts).Minutes + ":" + dte.Subtract(dts).Seconds;
                        rowIndex++;
                        viewList.Insert(rowIndex + 1, viewTemp);
                    }
                    index++;
                }
            }
        }

        /// <summary>
        /// 查询时间改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBox_time_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetDataSource();
        }

        /// <summary>
        /// 添加行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GridView_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString().Trim();
            }
        }

        BindingList<ControlCount> viewList = new BindingList<ControlCount>();

        /// <summary>
        /// 浏览记录数据对象
        /// </summary>
        [Serializable]
        public class ControlCount
        {
            public ControlCount()
            {
                name =string.Empty;
                openTime =string.Empty;
                closeTime =string.Empty;
                totalTime =string.Empty;
            }

            public string name { get; set; }
            public string openTime { get; set; }
            public string closeTime { get; set; }
            public string totalTime { get; set; }

            public bool IsNoView()
            {
                if (totalTime == string.Empty)
                    return true;
                else
                    return false;
            }
        }
    }
}